package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.data.MessageInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.SysMsgDetailTask;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by Administrater on 2015/12/28.
 */
public class SysMsgDetailActivity extends BaseActivity {
    private Context mContext;
    private String msgId;
    //private TextView msgTitle;
   // private TextView msgTime;
    private WebView msgContent;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sysmsgdetail);
        mContext=this;
        initView();
    }

    private void initView(){
        Intent intent = getIntent();
        msgId = intent.getStringExtra("ID");
       // msgTitle = (TextView) findViewById(R.id.sys_title);
      //  msgTime = (TextView) findViewById(R.id.sys_post_time);
        msgContent = (WebView) findViewById(R.id.sys_content);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        getSysMsgDetail();
    }

    private void getSysMsgDetail(){
        SysMsgDetailTask task = new SysMsgDetailTask(msgId);
        task.addListener(new GetDetailListner(mContext));
        getEatShowApp().getTaskManager().addTask(task);
    }

    class GetDetailListner extends ShowToastTaskEventListener{

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(data!=null){
                MessageInfo info = (MessageInfo) data;
                //msgTitle.setText(info.getMsgTitle());
               // msgTime.setText(info.getMsgTime());
               // msgContent.getSettings().setDefaultTextEncodingName("UTF-8") ;
                if(!TextUtils.isEmpty(info.getMsgContent())){
                    try {
                        msgContent.loadDataWithBaseURL(null, URLDecoder.decode(info.getMsgContent(),"utf-8"), "text/html", "charset=UTF-8", null);
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);

        }

        public GetDetailListner(Context context) {
            super(context);
        }
    }
}
